﻿namespace HZY.Shared.Admin.ApplicationServices;

/// <summary>
/// 后台系统基础控制器
/// </summary>
/// <typeparam name="TService"></typeparam>
/// <typeparam name="TEntity"></typeparam>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TSearchDto"></typeparam>
/// <typeparam name="TSaveFormDto"></typeparam>
public abstract class ApplicationControllerBase<TService, TEntity, TKey, TSearchDto, TSaveFormDto>(
    IServiceProvider serviceProvider) :
    ApplicationControllerBase<TService>(serviceProvider)
    where TService : ApplicationService<TEntity, TKey, TSearchDto, TSaveFormDto>
    where TEntity : class, new()
    where TSearchDto : class, new()
    where TSaveFormDto : class, new()
{
    /// <summary>
    /// 获取列表
    /// </summary>
    /// <param name="pagingSearchInput"></param>
    /// <returns></returns>
    [ActionDescriptor(PermissionFunctionConsts.Function_Search, DisplayName = "查看数据列表")]
    [ApiResourceCacheFilter(1)]
    [Microsoft.AspNetCore.Mvc.HttpPost]
    public virtual async Task<PagingView> FindListAsync([FromBody] PagingSearchInput<TSearchDto> pagingSearchInput)
    {
        return await Service.FindListAsync(pagingSearchInput);
    }

    /// <summary>
    /// 根据id数组删除
    /// </summary>
    /// <param name="ids"></param>
    /// <returns></returns>
    [ActionDescriptor(PermissionFunctionConsts.Function_Delete, DisplayName = "删除数据")]
    [Microsoft.AspNetCore.Mvc.HttpPost]
    public virtual async Task<bool> DeleteListAsync([FromBody] List<TKey> ids)
    {
        await Service.DeleteListAsync(ids);
        return true;
    }

    /// <summary>
    /// 查询表单数据
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [ActionDescriptor(PermissionFunctionConsts.Function_Search, DisplayName = "查看表单")]
    [Microsoft.AspNetCore.Mvc.HttpGet("{id?}")]
    public virtual Task<Dictionary<string, object?>> FindFormAsync([FromRoute] TKey id)
    {
        return Service.FindFormAsync(id);
    }

    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="form"></param>
    /// <returns></returns>
    [RequestLimitFilter]
    [ActionDescriptor(PermissionFunctionConsts.Function_Insert, DisplayName = "创建表单")]
    [Microsoft.AspNetCore.Mvc.HttpPost]
    [ApiCheckModel]
    public virtual Task CreateAsync([FromBody] TSaveFormDto form)
    {
        return Service.SaveFormAsync(form);
    }

    /// <summary>
    /// 编辑
    /// </summary>
    /// <param name="form"></param>
    /// <returns></returns>
    [RequestLimitFilter]
    [ActionDescriptor(PermissionFunctionConsts.Function_Update, DisplayName = "编辑表单")]
    [Microsoft.AspNetCore.Mvc.HttpPost]
    [ApiCheckModel]
    public virtual Task UpdateAsync([FromBody] TSaveFormDto form)
    {
        return Service.SaveFormAsync(form);
    }

    /// <summary>
    /// 导出Excel
    /// </summary>
    /// <param name="pagingSearchInput"></param>
    /// <returns></returns>
    [ActionDescriptor(DisplayName = "导出数据")]
    [ApiResourceCacheFilter(10)]
    [Microsoft.AspNetCore.Mvc.HttpPost]
    public virtual async Task ExportExcelAsync([FromBody] PagingSearchInput<TSearchDto> pagingSearchInput)
        => HttpContext.DownLoadFile(await Service.ExportExcelAsync(pagingSearchInput),
            Tools.GetFileContentType[".xls"].ToStr(),
            $"{PermissionUtil.GetControllerDisplayName(GetType())}列表数据 {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}.xls");
}